﻿using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace AOPCompiler
{
    static class SyntaxNodeExtensions
    {
        public static bool IsAttribute(this ClassDeclarationSyntax classDeclarationSyntax, string attributeName)
        {
            if (classDeclarationSyntax.BaseList == null)
            {
                return false;
            }
            // TODO handle QualifiedName
            foreach (var typeSyntax in classDeclarationSyntax.BaseList.Types)
            {
                var identifierNameSyntax = typeSyntax as IdentifierNameSyntax;
                if (identifierNameSyntax != null && identifierNameSyntax.Identifier.ValueText == attributeName)
                {
                    return true;
                }
            }

            return false;
        }

        public static SyntaxList<StatementSyntax> GetMethodStatements(this ClassDeclarationSyntax classDeclarationSyntax, string methodName)
        {
            foreach (var member in classDeclarationSyntax.Members)
            {
                if (member.CSharpKind() == SyntaxKind.MethodDeclaration)
                {
                    var methodDeclaration = member as MethodDeclarationSyntax;
                    if (methodDeclaration.Identifier.ValueText == methodName)
                    {
                        return methodDeclaration.Body.Statements;
                    }
                }
            }

            return SyntaxFactory.List<StatementSyntax>();
        }
    }
}
